<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>出入库单据列表 - 虚拟仓系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <!-- 统一导航栏 -->
    {% include 'includes/navbar.html' %}

    <div class="container mt-4">
        <h3>出入库单据列表</h3>
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="input-group">
                    <select id="docTypeFilter" class="form-select">
                        <option value="">所有类型</option>
                        <option value="IN">入库单</option>
                        <option value="OUT">出库单</option>
                    </select>
                    <select id="warehouseFilter" class="form-select">
                        <option value="">所有仓库</option>
                        <option value="华东仓">华东仓</option>
                        <option value="华南仓">华南仓</option>
                    </select>
                    <button id="searchBtn" class="btn btn-primary">查询</button>
                </div>
            </div>
        </div>
        <div class="card">
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>单据编号</th>
                            <th>类型</th>
                            <th>仓库</th>
                            <th>操作人</th>
                            <th>创建时间</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="docsTable"></tbody>
                </table>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 加载单据列表（带/api前缀）
        const loadDocs = async () => {
            const docType = document.getElementById('docTypeFilter').value;
            const warehouse = document.getElementById('warehouseFilter').value;
            const res = await fetch(`/api/stock/docs?doc_type=${encodeURIComponent(docType)}&warehouse=${encodeURIComponent(warehouse)}`);
            const docs = await res.json();
            const table = document.getElementById('docsTable');
            table.innerHTML = docs.map(doc => `
                <tr>
                    <td>${doc.doc_id}</td>
                    <td>${doc.doc_type}</td>
                    <td>${doc.warehouse}</td>
                    <td>${doc.operator}</td>
                    <td>${doc.create_time}</td>
                    <td><span class="badge bg-success">${doc.status}</span></td>
                    <td>
                        <a href="/stock/docs/${doc.doc_id}" class="btn btn-sm btn-info">查看详情</a>
                    </td>
                </tr>
            `).join('');
        };

        document.getElementById('searchBtn').addEventListener('click', loadDocs);
        document.getElementById('logout').addEventListener('click', () => window.location.href = '/login_page');
        window.onload = loadDocs;
    </script>
</body>
</html>